/**
 *  设置用户登录的权限列表
 * @param {权限列表} authority
 */
export function setAuthority(authority) {
  localStorage.setItem('loginUserAuthority', authority);
}

/**
 *  清除用户登录权限
 */
export function clearAuthority() {
  localStorage.removeItem('loginUserAuthority');
}

/**
 *  获取当前用户的登录权限列表数据
 */
export function getAuthority() {
  return localStorage.getItem('loginUserAuthority');
}
/* 设置菜单列表 */
export function setMenu(list) {
  localStorage.setItem('menuList', list);
}
export function getMenu() {
  return localStorage.getItem('menuList');
}
export function clearMenu() {
  localStorage.removeItem('menuList');
}

/**
 *  设置用户登录凭证
 * @param {登录凭证} token
 */
export function setLoginToken(token) {
  localStorage.setItem('loginUserToken', token);
}

export function setUserInfo(userInfo) {
  localStorage.setItem('userInfo', JSON.stringify(userInfo));
}

/**
 *  清除用户登录凭证
 */
export function clearLoginToken() {
  localStorage.removeItem('loginUserToken');
}

/**
 *  获取用户的登录凭证
 */
export function getLoginToken() {
  return localStorage.getItem('loginUserToken');
}

/**
 *  检查当前用户是否登录状态
 */
export function checkLogin() {
  let userToken = getLoginToken();
  if (userToken === null) {
    return false;
  }
  return true;
}

/**
 *  设置页面标题
 * @param {*} title
 */
export function setTitle(title) {
  document.title = title;
}
//时间转换
export function transformTimestamp(timestamp) {
  let a = new Date(timestamp).getTime();
  const date = new Date(a);
  const Y = date.getFullYear() + '-';
  const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  const D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + '  ';
  const h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  const m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  // const s = date.getSeconds(); // 秒
  const dateString = Y + M + D + h + m;
  // console.log('dateString', dateString); // > dateString 2021-07-06 14:23
  return dateString;
}

const currencyFormatter = new Intl.NumberFormat('en-US', {
  style: 'currency',
  currency: 'USD',
  minimumFractionDigits: 2
});

export function formatCurrency(val) {
  if (typeof val !== 'number') {
    val = Number(val);
  }
  if (!val && val !== 0) {
    return '--';
  }
  return currencyFormatter.format(val);
}

export function getShopId() {
  let userInfo = localStorage.getItem('userInfo');
  if (userInfo) {
    try {
      userInfo = JSON.parse(userInfo);
    } catch (ex) {
      return null;
    }
  }
  if (userInfo) {
    return userInfo.shopId;
  }
  return null;
}

export function getUUID(prefix) {
  return String(Math.random() + Math.random()).replace(/\d\.\d{4}/, prefix || 'rc');
}
